package com.twinkle.rpc.server.nettyConfig.nettyInitializer;

import com.twinkle.rpc.common.serializer.mycoder.MyDecoder;
import com.twinkle.rpc.common.serializer.mycoder.MyEncoder;
import com.twinkle.rpc.common.serializer.myserializer.JsonSerializer;
import com.twinkle.rpc.common.serializer.myserializer.ObjectSerializer;
import com.twinkle.rpc.server.nettyConfig.handler.NettyRPCServerHandler;
import com.twinkle.rpc.server.provider.ServiceProvider;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.AllArgsConstructor;

/**
 * @author twinkle
 * 定义channel处理数据的格式
 * @version 1.0
 */
@AllArgsConstructor
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {
    private ServiceProvider serviceProvider;
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //使用自定义的编/解码器
        pipeline.addLast(new MyEncoder(new JsonSerializer()));
        pipeline.addLast(new MyDecoder());
        pipeline.addLast(new NettyRPCServerHandler(serviceProvider));
    }
}
